package com.diaohw.module.metadata.service.meta;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diaohw.module.metadata.mapper.MetaRelationshipMapper;
import com.diaohw.module.metadata.obj.dto.MetaRelationshipDTO;
import com.diaohw.module.metadata.obj.entity.MetaRelationshipDO;
import com.diaohw.module.metadata.obj.query.MetaRelationshipQuery;
import com.diaohw.module.metadata.obj.vo.MetaRelationshipVO;
import com.diaohw.module.metadata.service.MetaRelationshipService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.framework.db.utils.WrapperUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 元数据分组关联关系表 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2025-04-07
 */
@Service
public class MetaRelationshipImpl extends ServiceImpl<MetaRelationshipMapper, MetaRelationshipDO> implements MetaRelationshipService {

    @Override
    public MetaRelationshipVO create(MetaRelationshipDTO data) {
        MetaRelationshipDO model = INSTANCE.dtoToEntity(data);
        this.save(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public MetaRelationshipVO update(String id, MetaRelationshipDTO data) {
        MetaRelationshipDO model = INSTANCE.dtoToEntity(data);
        model.setId(id);
        this.updateById(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public Boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public MetaRelationshipVO detail(String id) {
        MetaRelationshipDO model = this.getById(id);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public List<MetaRelationshipVO> findByList(MetaRelationshipQuery query) {
        LambdaQueryWrapper<MetaRelationshipDO> wrapper = createWrapper(query);
        List<MetaRelationshipDO> list = this.list(wrapper);
        return INSTANCE.entityToVo(list);
    }

    @Override
    public PageResultVo<MetaRelationshipVO> findByPage(MetaRelationshipQuery query) {
        LambdaQueryWrapper<MetaRelationshipDO> wrapper = createWrapper(query);
        Page<MetaRelationshipDO> page = this.page(WrapperUtils.createPage(query), wrapper);
        List<MetaRelationshipVO> list = INSTANCE.entityToVo(page.getRecords());
        return PageResultVo.create(page.getTotal(), list);
    }

    /**
    * 生成查询条件
    *
    * @param query 查询条件
    */
    private LambdaQueryWrapper<MetaRelationshipDO> createWrapper(MetaRelationshipQuery query) {
        LambdaQueryWrapper<MetaRelationshipDO> wrapper = Wrappers.lambdaQuery(MetaRelationshipDO.class);
        wrapper.like(StrUtil.isNotBlank(query.getGroupId()), MetaRelationshipDO::getGroupId, query.getGroupId());
        wrapper.like(StrUtil.isNotBlank(query.getGroupName()), MetaRelationshipDO::getGroupName, query.getGroupName());
        wrapper.like(StrUtil.isNotBlank(query.getGroupCode()), MetaRelationshipDO::getGroupCode, query.getGroupCode());
        wrapper.like(StrUtil.isNotBlank(query.getMetaId()), MetaRelationshipDO::getMetaId, query.getMetaId());
        wrapper.like(StrUtil.isNotBlank(query.getMetaName()), MetaRelationshipDO::getMetaName, query.getMetaName());
        wrapper.like(StrUtil.isNotBlank(query.getMetaCode()), MetaRelationshipDO::getMetaCode, query.getMetaCode());
        return wrapper;
    }

}
